package com.dataseek.iot.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dataseek.iot.product.entity.PdtAttributeDic;
import com.dataseek.iot.product.entity.PdtEventRel;
import com.dataseek.iot.product.mapper.PdtAttributeDicMapper;
import com.dataseek.iot.product.mapper.PdtEventRelMapper;
import com.dataseek.iot.product.service.PdtEventRelService;
import com.dataseek.iot.product.vo.PdtEventRelVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.service.IDServiceHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @discription:
 * @author: Freya.Yan
 * @date: 2022/1/11
 */
@Service
@Slf4j
public class PdtEventRelServiceImpl implements PdtEventRelService {
    @Autowired
    private IDServiceHelper idServiceHelper;

    @Autowired
    private PdtEventRelMapper pdtEventRelMapper;

    @Autowired
    private PdtAttributeDicMapper pdtAttributeDicMapper;


    @Override
    public PdtEventRelVo queryPdtEventRel(String pdtCode, String eventName, Integer num, Integer size) {
        List<PdtEventRel> pdtEventRelList = new ArrayList<>();
        PdtEventRelVo pdtListVo = new PdtEventRelVo();
        QueryWrapper<PdtEventRel> qw = new QueryWrapper<>();
        if (pdtCode != null && !pdtCode.isEmpty()) {
            qw.like("pdt_code", pdtCode);
        }
        if (eventName != null && !eventName.isEmpty()) {
            qw.like("event_name", eventName);
        }
        IPage<PdtEventRel> page = new Page<>(num, size);
        List<PdtEventRel> mapList = pdtEventRelMapper.selectPage(page, qw).getRecords();
        for (int i = 0;i<mapList.size();i++) {
            PdtEventRel pdtEventRel = mapList.get(i);
            String attrCode = pdtEventRel.getAttCode();
            QueryWrapper<PdtAttributeDic> qwAttr = new QueryWrapper<>();
            qwAttr.eq("att_code", attrCode);
            PdtAttributeDic pdtAttributeDic = pdtAttributeDicMapper.selectOne(qwAttr);
            pdtEventRel.setAttName(pdtAttributeDic.getAttName());
            pdtEventRel.setAttType(pdtAttributeDic.getAttType());
            pdtEventRel.setDataType(pdtAttributeDic.getDataType());
            pdtEventRelList.add(pdtEventRel);
        }
        pdtListVo.setPage(num);
        pdtListVo.setSize(size);
        pdtListVo.setTotal(page.getTotal());
        pdtListVo.setList(pdtEventRelList);
        return pdtListVo;
    }

    //新增部门信息
    @Override
    public Integer insertPdtEventRel(PdtEventRel pdtEventRel) {
        int resultCode = 0;
        long id = idServiceHelper.nextId();
        pdtEventRel.setId(id);
        String code = pdtEventRel.getAttCode();
        QueryWrapper<PdtEventRel> qw = new QueryWrapper<>();
        qw.eq("att_code", code);
        Integer count = pdtEventRelMapper.selectCount(qw);
        if (count == 0) {
            pdtEventRelMapper.insert(pdtEventRel);
        } else {
            resultCode = RspStatusEnum.FAIL.getCode();
        }
        return resultCode;

    }

    @Override
    public Integer updatePdtEventRel(PdtEventRel pdtEventRel) {
        int resultCode = pdtEventRelMapper.updateById(pdtEventRel);
        return resultCode;
    }

    @Override
    public Integer deletePdtEventRel(List<Long> ids) {
        Integer resultCode = 0;
        resultCode = pdtEventRelMapper.deleteBatchIds(ids);
        return resultCode;
    }

}
